package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EnterpriseEmployee
import cn.exrick.xboot.base.entity.SecurityAdvisorJobTrain
import cn.exrick.xboot.base.mapper.SecurityAdvisorJobTrainMapper
import cn.exrick.xboot.base.service.*
import cn.exrick.xboot.base.vo.SecurityAdvisorJobTrainVo
import cn.exrick.xboot.base.vo.SecurityAdvisorTrainingInfoVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.UserService
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 安全顾问岗位培训上传接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISecurityAdvisorJobTrainServiceImpl : ServiceImpl<SecurityAdvisorJobTrainMapper?, SecurityAdvisorJobTrain?>(), ISecurityAdvisorJobTrainService {

    @Autowired
    private var securityUtil: SecurityUtil? = null

    @Autowired
    private var iJobService: IJobService? = null

    @Autowired
    private var iEnterpriseUserService:IEnterpriseUserService? = null

    @Autowired
    private var userService:UserService? = null

    @Autowired
    private lateinit var iEnterpriseEmployeeService: IEnterpriseEmployeeService

    @Autowired
    private lateinit var iEnterpriseEmployeeRecordService: IEnterpriseEmployeeRecordService


    override fun createOrUpdateJobTrain(securityAdvisorJobTrain: SecurityAdvisorJobTrain): Result<Any?>? {

        return try {

            saveOrUpdate(securityAdvisorJobTrain)
            ResultUtil.success<Any>("操作成功！")
        }catch (e: Exception) {

            ResultUtil.error<Any>(e.message)
        }
    }

    override fun removeJobTrain(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success<Any>("操作成功！")
        }catch (e: Exception) {

            ResultUtil.error<Any>(e.message)
        }
    }

    override fun getJobTrainVo(id: String): SecurityAdvisorJobTrainVo? {

        return getById(id)?.run {

            SecurityAdvisorJobTrainVo().apply {

                ToolUtil.copyProperties(this@run, this)
                trainPicsList = trainPics?.split(",")
                trainPicsFirst = trainPicsList?.get(0)
                iJobService!!.getById(jobId)?.let {

                    jobName = it.jobName
                    enterpriseId = it.enterpriseId
                    enterpriseUserName = userService!!.get(it.createBy)?.nickname
                }
            }
        }
    }

    override fun getJobTrainList(pageVo: PageVo, jobType: Int?, enterpriseId: String?, jobId: String?, roleType: Int?): Result<MutableMap<String, Any?>>? {

       return ToolUtil.setListToNul(list(KtQueryWrapper(SecurityAdvisorJobTrain::class.java)
               .eq(ToolUtil.isNotEmpty(jobType),SecurityAdvisorJobTrain::jobType, jobType)
               .eq(ToolUtil.isNotEmpty(enterpriseId), SecurityAdvisorJobTrain::enterpriseId, enterpriseId)
               .eq(ToolUtil.isNotEmpty(roleType), SecurityAdvisorJobTrain::jobId, iEnterpriseEmployeeService.getOne(KtQueryWrapper(EnterpriseEmployee::class.java)
                              .eq(EnterpriseEmployee::userId, securityUtil!!.currUser.id))?.jobId)
               .eq(ToolUtil.isEmpty(roleType), SecurityAdvisorJobTrain::createBy,securityUtil!!.currUser.id)
                .orderByDesc(SecurityAdvisorJobTrain::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getJobTrainVo(it?.id!!) }.toMutableList())

            ResultUtil.data(result, "获取数据成功")
        } ?: ResultUtil.error(201,"暂无数据!")
    }

    override fun getEmployeeTrainingDetail(employeeId: String): Result<SecurityAdvisorTrainingInfoVo>? {

        return ResultUtil.data(SecurityAdvisorTrainingInfoVo().apply {

            enterpriseEmployeeTrainingVo = iEnterpriseEmployeeService.getEmployeeTrainingInfo(employeeId)

            //获取该员工最近的一场培训考试记录
            iEnterpriseEmployeeRecordService.getEmployeeExamRecordByUserIdAndLast(employeeId)?.let {

                ToolUtil.copyProperties(it, this@apply)
            }

            //考试完成进度
        }, "获取企业员工培训详情成功！")
    }
}